In [15]:
import os
from shutil import copyfile

import fastai
from fastai.vision import *
from fastai.callbacks import *
from fastai.utils.mem import *

import torch
from torchvision.models import vgg16_bn
import warnings; warnings.simplefilter('ignore')
In [18]:
torch.cuda.is_available()
Out[18]:
True
In [17]:
torch.cuda.device(0)
Out[17]:
<torch.cuda.device at 0x7f70b3dc1e90>
In [2]:
data_dir = "/home/ubuntu/data"
In [3]:
os.listdir(data_dir)
Out[3]:
['games',
 'full-pilled',
 'pilled',
 'bottom',
 'unpilled',
 'full',
 'top',
 '.ipynb_checkpoints']

Collect Top & Bottom

In [4]:
# games_dir = data_dir + "/games/"
# games = os.listdir(games_dir)
# for g in games:
#     top_dir = games_dir + g +"/top/"
#     top_images = os.listdir(top_dir)
#     for img in top_images:
#         if ".jpg" in img:
#             copyfile(top_dir + img, data_dir + "/top/"+img)

# for g in games:
#     bottom_dir = games_dir + g +"/bottom/"
#     bottom_images = os.listdir(bottom_dir)
#     for img in bottom_images:
#         if ".jpg" in img:
#             copyfile(bottom_dir + img, data_dir + "/bottom/"+img)
In [5]:
path_source = os.path.normpath(data_dir + '/bottom')
path_current = os.path.normpath(data_dir + '/bottom')
path_future = os.path.normpath(data_dir + '/full-pilled')
In [6]:
bs,size=16, 200
arch = models.resnet34
In [7]:
arch = models.resnet34
src = ImageImageList.from_folder(path_current).split_by_rand_pct(0.15, seed=42)
In [8]:
def get_data(bs,size):
    data = (src.label_from_func(lambda x: path_future+"/"+x.name)
           .transform([], size=size, tfm_y=True)
           .databunch(bs=bs).normalize(imagenet_stats, do_y=True))

    data.c = 3
    return data
In [9]:
data_gen = get_data(bs,size)
In [10]:
data_gen.show_batch(4)
In [11]:
wd = 1e-3
y_range = (-3.,3.)
loss_gen = MSELossFlat()
In [12]:
def create_gen_learner():
    return unet_learner(data_gen, arch, wd=wd, blur=True, norm_type=NormType.Weight,
                         self_attention=True, y_range=y_range, loss_func=loss_gen)
In [19]:
learn_gen = create_gen_learner()
gc.collect()
Out[19]:
0
In [20]:
learn_gen.fit_one_cycle(12, pct_start=0.8)
learn_gen.show_results(rows=2)
80.00% [16/20 27:14<06:48]
epoch train_loss valid_loss time
0 0.664439 0.493275 01:41
1 0.451471 0.384158 01:42
2 0.369483 0.337346 01:42
3 0.316671 0.289686 01:42
4 0.274754 0.254954 01:43
5 0.251565 0.246332 01:43
6 0.240068 0.236756 01:42
7 0.249640 0.235320 01:42
8 0.231244 0.238687 01:42
9 0.228548 0.223733 01:42
10 0.222886 0.260610 01:41
11 0.231151 0.220784 01:41
12 0.207848 0.209559 01:41
13 0.216422 0.209923 01:41
14 0.193568 0.201882 01:40
15 0.188064 0.204609 01:40

Interrupted
Traceback (most recent call last):
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/queues.py", line 242, in _feed
    send_bytes(obj)
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 200, in send_bytes
    self._send_bytes(m[offset:offset + size])
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 404, in _send_bytes
    self._send(header + buf)
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 368, in _send
    n = write(self._handle, buf)
BrokenPipeError: [Errno 32] Broken pipe
Traceback (most recent call last):
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/queues.py", line 242, in _feed
    send_bytes(obj)
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 200, in send_bytes
    self._send_bytes(m[offset:offset + size])
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 404, in _send_bytes
    self._send(header + buf)
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 368, in _send
    n = write(self._handle, buf)
BrokenPipeError: [Errno 32] Broken pipe
Traceback (most recent call last):
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/queues.py", line 242, in _feed
    send_bytes(obj)
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 200, in send_bytes
    self._send_bytes(m[offset:offset + size])
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 404, in _send_bytes
    self._send(header + buf)
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 368, in _send
    n = write(self._handle, buf)
BrokenPipeError: [Errno 32] Broken pipe
Traceback (most recent call last):
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/queues.py", line 242, in _feed
    send_bytes(obj)
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 200, in send_bytes
    self._send_bytes(m[offset:offset + size])
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 404, in _send_bytes
    self._send(header + buf)
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 368, in _send
    n = write(self._handle, buf)
BrokenPipeError: [Errno 32] Broken pipe
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-20-7ed9d6a3bcea> in <module>
----> 1 learn_gen.fit_one_cycle(20, pct_start=0.8)
      2 learn_gen.show_results(rows=2)

~/anaconda3/lib/python3.7/site-packages/fastai/train.py in fit_one_cycle(learn, cyc_len, max_lr, moms, div_factor, pct_start, final_div, wd, callbacks, tot_epochs, start_epoch)
     21     callbacks.append(OneCycleScheduler(learn, max_lr, moms=moms, div_factor=div_factor, pct_start=pct_start,
     22                                        final_div=final_div, tot_epochs=tot_epochs, start_epoch=start_epoch))
---> 23     learn.fit(cyc_len, max_lr, wd=wd, callbacks=callbacks)
     24 
     25 def fit_fc(learn:Learner, tot_epochs:int=1, lr:float=defaults.lr,  moms:Tuple[float,float]=(0.95,0.85), start_pct:float=0.72,

~/anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in fit(self, epochs, lr, wd, callbacks)
    198         else: self.opt.lr,self.opt.wd = lr,wd
    199         callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(callbacks)
--> 200         fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)
    201 
    202     def create_opt(self, lr:Floats, wd:Floats=0.)->None:

~/anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in fit(epochs, learn, callbacks, metrics)
     99             for xb,yb in progress_bar(learn.data.train_dl, parent=pbar):
    100                 xb, yb = cb_handler.on_batch_begin(xb, yb)
--> 101                 loss = loss_batch(learn.model, xb, yb, learn.loss_func, learn.opt, cb_handler)
    102                 if cb_handler.on_batch_end(loss): break
    103 

~/anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in loss_batch(model, xb, yb, loss_func, opt, cb_handler)
     28 
     29     if not loss_func: return to_detach(out), to_detach(yb[0])
---> 30     loss = loss_func(out, *yb)
     31 
     32     if opt is not None:

~/anaconda3/lib/python3.7/site-packages/fastai/layers.py in __call__(self, input, target, **kwargs)
    241         if self.floatify: target = target.float()
    242         input = input.view(-1,input.shape[-1]) if self.is_2d else input.view(-1)
--> 243         return self.func.__call__(input, target.view(-1), **kwargs)
    244 
    245 def CrossEntropyFlat(*args, axis:int=-1, **kwargs):

~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    539             result = self._slow_forward(*input, **kwargs)
    540         else:
--> 541             result = self.forward(*input, **kwargs)
    542         for hook in self._forward_hooks.values():
    543             hook_result = hook(self, input, result)

~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
    429 
    430     def forward(self, input, target):
--> 431         return F.mse_loss(input, target, reduction=self.reduction)
    432 
    433 

~/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py in mse_loss(input, target, size_average, reduce, reduction)
   2202     else:
   2203         expanded_input, expanded_target = torch.broadcast_tensors(input, target)
-> 2204         ret = torch._C._nn.mse_loss(expanded_input, expanded_target, _Reduction.get_enum(reduction))
   2205     return ret
   2206 

KeyboardInterrupt: 
In [21]:
learn_gen.unfreeze()
In [22]:
learn_gen.fit_one_cycle(30, slice(1e-6,1e-3))
learn_gen.show_results(rows=10)
93.33% [28/30 1:05:07<04:39]
epoch train_loss valid_loss time
0 0.162961 0.188470 02:19
1 0.161230 0.186727 02:19
2 0.157522 0.185093 02:19
3 0.154015 0.182960 02:19
4 0.149830 0.188223 02:19
5 0.152622 0.184060 02:19
6 0.147444 0.183982 02:19
7 0.146758 0.183719 02:19
8 0.145144 0.180529 02:19
9 0.144663 0.181936 02:19
10 0.142342 0.185256 02:19
11 0.150442 0.185564 02:19
12 0.137240 0.181295 02:19
13 0.130089 0.176910 02:19
14 0.125586 0.173130 02:19
15 0.125949 0.173721 02:19
16 0.115514 0.175194 02:19
17 0.107502 0.171359 02:19
18 0.104537 0.168143 02:19
19 0.101197 0.167318 02:19
20 0.098019 0.168933 02:19
21 0.096683 0.171690 02:19
22 0.092572 0.170214 02:19
23 0.088839 0.168726 02:19
24 0.086427 0.170522 02:19
25 0.083684 0.169958 02:19
26 0.082051 0.170002 02:19
27 0.080702 0.170256 02:19

Interrupted
Traceback (most recent call last):
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/queues.py", line 242, in _feed
    send_bytes(obj)
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 200, in send_bytes
    self._send_bytes(m[offset:offset + size])
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 404, in _send_bytes
    self._send(header + buf)
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 368, in _send
    n = write(self._handle, buf)
BrokenPipeError: [Errno 32] Broken pipe
Traceback (most recent call last):
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/queues.py", line 242, in _feed
    send_bytes(obj)
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 200, in send_bytes
    self._send_bytes(m[offset:offset + size])
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 404, in _send_bytes
    self._send(header + buf)
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 368, in _send
    n = write(self._handle, buf)
BrokenPipeError: [Errno 32] Broken pipe
Traceback (most recent call last):
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/queues.py", line 242, in _feed
    send_bytes(obj)
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 200, in send_bytes
    self._send_bytes(m[offset:offset + size])
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 404, in _send_bytes
    self._send(header + buf)
  File "/home/ubuntu/anaconda3/lib/python3.7/multiprocessing/connection.py", line 368, in _send
    n = write(self._handle, buf)
BrokenPipeError: [Errno 32] Broken pipe
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-22-fe859efd4783> in <module>
----> 1 learn_gen.fit_one_cycle(30, slice(1e-6,1e-3))
      2 learn_gen.show_results(rows=10)

~/anaconda3/lib/python3.7/site-packages/fastai/train.py in fit_one_cycle(learn, cyc_len, max_lr, moms, div_factor, pct_start, final_div, wd, callbacks, tot_epochs, start_epoch)
     21     callbacks.append(OneCycleScheduler(learn, max_lr, moms=moms, div_factor=div_factor, pct_start=pct_start,
     22                                        final_div=final_div, tot_epochs=tot_epochs, start_epoch=start_epoch))
---> 23     learn.fit(cyc_len, max_lr, wd=wd, callbacks=callbacks)
     24 
     25 def fit_fc(learn:Learner, tot_epochs:int=1, lr:float=defaults.lr,  moms:Tuple[float,float]=(0.95,0.85), start_pct:float=0.72,

~/anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in fit(self, epochs, lr, wd, callbacks)
    198         else: self.opt.lr,self.opt.wd = lr,wd
    199         callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(callbacks)
--> 200         fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)
    201 
    202     def create_opt(self, lr:Floats, wd:Floats=0.)->None:

~/anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in fit(epochs, learn, callbacks, metrics)
     99             for xb,yb in progress_bar(learn.data.train_dl, parent=pbar):
    100                 xb, yb = cb_handler.on_batch_begin(xb, yb)
--> 101                 loss = loss_batch(learn.model, xb, yb, learn.loss_func, learn.opt, cb_handler)
    102                 if cb_handler.on_batch_end(loss): break
    103 

~/anaconda3/lib/python3.7/site-packages/fastai/basic_train.py in loss_batch(model, xb, yb, loss_func, opt, cb_handler)
     32     if opt is not None:
     33         loss,skip_bwd = cb_handler.on_backward_begin(loss)
---> 34         if not skip_bwd:                     loss.backward()
     35         if not cb_handler.on_backward_end(): opt.step()
     36         if not cb_handler.on_step_end():     opt.zero_grad()

~/anaconda3/lib/python3.7/site-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)
    148                 products. Defaults to ``False``.
    149         """
--> 150         torch.autograd.backward(self, gradient, retain_graph, create_graph)
    151 
    152     def register_hook(self, hook):

~/anaconda3/lib/python3.7/site-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
     97     Variable._execution_engine.run_backward(
     98         tensors, grad_tensors, retain_graph, create_graph,
---> 99         allow_unreachable=True)  # allow_unreachable flag
    100 
    101 

KeyboardInterrupt: 
In [23]:
learn_gen.show_results(rows=60)
In [ ]: